﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.HangHoa;
using RTSoft.Modules;
using DevExpress.XtraEditors.Repository;
using RTSoft.SoureCode.Control.LoaiHang;
using RTSoft.SoureCode.Control.XuatXu;
using RTSoft.SoureCode.Control.NhaSanXuat;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmMatHang : DevExpress.XtraEditors.XtraForm
    {
        public frmMatHang()
        {
            InitializeComponent();
        }

        DataTable tbl;
        String errConnect;
        String errSQL;

        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            hanghoactrl ctrl = new hanghoactrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_MATHANG"].Caption = "ID";
            GridView1.Columns["ID_NSX"].Caption = "Nhà Sản Xuất";
            GridView1.Columns["ID_XX"].Caption = "Xuất Xứ";
            GridView1.Columns["ID_LOAI"].Caption = "Loại Hàng";
            GridView1.Columns["TEN_MATHANG"].Caption = "Tên Hàng";
            GridView1.Columns["DONVITINH"].Caption = "ĐVT";
            GridView1.Columns["SOLUONG_MIN"].Caption = "Số lượng còn lại";
            GridView1.Columns["VAT"].Caption = "VAT (%)";
            GridView1.Columns["SOLUONG_MAX"].Visible = false;
            //GridView1.Columns["SOLUONG_MIN"].Visible = false;
            GridView1.Columns["TRANGTHAI"].Visible = false;
            GridView1.Columns["ID_MATHANG"].Visible = false;

            // Chỉnh loại hàng thành combobox
            RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
            loaihangctrl lctrl = new loaihangctrl();
            DataTable tblLoai = new DataTable();
            lctrl.getData(ref errConnect, ref errSQL,ref tblLoai);
            riCombo.DataSource = tblLoai;
            riCombo.ValueMember = "ID_LOAI";
            riCombo.DisplayMember = "TEN_LOAI";
            riCombo.View.Columns["TEN_LOAI"].Caption = "Loại";
            riCombo.View.Columns["ID_LOAI"].Visible = false;
            riCombo.View.Columns["TRANG_THAI"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_LOAI"].ColumnEdit = riCombo;

            // Chỉnh xuất xứ thành combobox
            riCombo = new RepositoryItemGridLookUpEdit();
            xuatxuctrl xctrl = new xuatxuctrl();
            DataTable tblXuatxu = new DataTable();
            xctrl.getData(ref errConnect, ref errSQL, ref tblXuatxu);
            riCombo.DataSource = tblXuatxu;
            riCombo.ValueMember = "ID_XX";
            riCombo.DisplayMember = "TENNUOC";
            riCombo.View.Columns["TENNUOC"].Caption = "Xuất xứ";
            riCombo.View.Columns["ID_XX"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_XX"].ColumnEdit = riCombo;

            // Chỉnh nhà sản xuất thành combobox
            riCombo = new RepositoryItemGridLookUpEdit();
            nhasxctrl nsxctrl = new nhasxctrl();
            DataTable tblNsx = new DataTable();
            nsxctrl.getData(ref errConnect, ref errSQL, ref tblNsx);
            riCombo.DataSource = tblNsx;
            riCombo.ValueMember = "ID_NSX";
            riCombo.DisplayMember = "TEN_NSX";
            riCombo.View.Columns["TEN_NSX"].Caption = "Nhà Sản Xuất";
            riCombo.View.Columns["ID_NSX"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_NSX"].ColumnEdit = riCombo;

            GridView1.Columns["TEN_MATHANG"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            
            GridView1.BestFitColumns();
        }

        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Danh Sách Mặt Hàng", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.FocusedRowHandle >= 0)
                {
                    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_MATHANG"].FieldName));
                    errConnect = "";
                    errSQL = "";
                    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["TEN_MATHANG"].FieldName).ToString();
                    Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa " + strName + " ?");
                    if (result == false)
                    {
                        return;
                    }
                    hanghoactrl ctrl = new hanghoactrl();
                    ctrl.Delete(ref errConnect, ref errSQL, id);
                    if (errSQL == "")
                    {
                        GridView1.DeleteSelectedRows();
                        GridView1.RefreshData();
                    }
                    else {
                        Library.ShowMessage("Không thể xóa mặt hàng này");
                    }
                    
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            
            String colTemp = "";
            try
            {
                // Bắt lỗi dữ liệu rỗng
                for (int i = 0; i < GridView1.Columns.Count - 1; i++)
                {
                    if (GridView1.Columns[i].Visible == true)
                    {
                        colTemp = (GridView1.Columns[i].Name).Substring(3);
                        Object value = GridView1.GetRowCellValue(e.RowHandle, colTemp);
                        if ((colTemp == "ID_NSX") || (colTemp == "ID_XX") || (colTemp == "TEN_MATHANG") || (colTemp == "ID_LOAI") || (colTemp == "SOLUONG_MAX"))
                        {
                            if (value == System.DBNull.Value)
                            {
                                e.ErrorText = "Hãy Nhập " + GridView1.Columns[i].Caption;
                                e.Valid = false;
                                GridView1.FocusedColumn = GridView1.Columns[colTemp];
                                GridView1.ShowEditor();
                                return;
                            }
                        }
                    }
                }
                hanghoactrl ctrl = new hanghoactrl();
                hanghoainfo info = new hanghoainfo();

                errConnect = "";
                errSQL = "";
                DataRow row = GridView1.GetDataRow(e.RowHandle);
                info.ID_MATHANG = Convert.ToInt32(RTValidate.CheckNull(row["ID_MATHANG"]));
                info.ID_NSX = Convert.ToInt32(RTValidate.CheckNull(row["ID_NSX"]));
                info.ID_XX = Convert.ToInt32(RTValidate.CheckNull(row["ID_XX"]));
                info.ID_LOAI = Convert.ToInt32(RTValidate.CheckNull(row["ID_LOAI"]));
                info.TEN_MATHANG =(RTValidate.EmptyNull(row["TEN_MATHANG"]).ToString());
                info.DONVITINH = RTValidate.EmptyNull(row["DONVITINH"]).ToString() ;
                info.SOLUONG_MAX = 0;
                info.SOLUONG_MIN = 0;
                info.VAT = Convert.ToInt32(RTValidate.CheckNull(row["VAT"]));
                info.TRANGTHAI = 0;
                if (info.ID_MATHANG != 0)
                {
                    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có muốn sửa?");
                    if (result != false)
                    {
                        ctrl.Update(ref errConnect, ref errSQL, info);
                    }
                }
                else
                {
                    ctrl.Insert(ref errConnect, ref errSQL, info);
                }
                this.InitGrid();
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void frmMatHang_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }
     
        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            try
            {
                int iRow = this.GridView1.FocusedRowHandle;
                if (iRow >= 0)
                {
                                       
                    txtID.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_MATHANG");
                    txtName.Text = this.GridView1.GetRowCellDisplayText(iRow, "TEN_MATHANG");
                    txtLoai.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_LOAI");
                    txtNSX.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_NSX");
                    txtXX.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_XX");
                    txtVAT.Text = this.GridView1.GetRowCellDisplayText(iRow, "VAT");
                    txtDVT.Text = this.GridView1.GetRowCellDisplayText(iRow, "DONVITINH");
                    txtSoluongMax.Text = this.GridView1.GetRowCellDisplayText(iRow, "SOLUONG_MAX");
                    txtSoluongMin.Text = this.GridView1.GetRowCellDisplayText(iRow, "SOLUONG_MIN");
                    int sl = Convert.ToInt32(this.GridView1.GetRowCellDisplayText(iRow, "SOLUONG_MIN"));
                    if (sl != 0)
                    {
                        txtStatus.ForeColor = Color.Blue;
                        txtStatus.Text = "Còn Hàng";
                    }
                    else 
                    {
                        txtStatus.ForeColor = Color.Red;
                        txtStatus.Text = "Hết hàng";
                    }
                }
                else
                {
                    txtID.Text = "";                   
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

      
    }
}